import {
  apiSetOrderStatus
} from '../../../other/api'

const { regeneratorRuntime } = global

Page(global.connect(
  state => ({
    ...state.globalReducer,
    ...state.activityReducer
  }),
  dispatch => ({
    handleRoute: global.handleRoute,
    async getOrderDetailByOrderNo(data) {
      return await dispatch(global.activityReducer.getOrderDetailByOrderNo(data))
    },
    async postAuditOrder(data) {
      return await dispatch(global.activityReducer.postAuditOrder(data))
    },
    // 获取活动详情
    async getActivityDetail(data) {
      return await dispatch(global.activityReducer.getActivityDetail(data))
    },
    async prepareEffectImage(data) {
      return await dispatch(global.globalReducer.prepareEffectImage(data))
    },
    // 订单状态类别
    async getOrderStatusList(data) {
      return await dispatch(global.activityReducer.getOrderStatusList(data))
    },
  })
)({
  data: {
    uiTabIndex: 0,
    uiShowSearch: false,
    uiMessage: '请填写不通过的原因',
    uiOrderType: 0,
    uiSteps: [
      { name: '物料下单', time: '2018-01-28  13:00', status: 1 },
      { name: '订单审核通过', time: '2018-01-28  13:00', status: 1 },
      { name: '订单已接受', time: '2018-01-28  13:00', status: 1 },
      { name: '师傅已到店', time: '', status: 0, uname: '唐某某', mobile: '18674520020' },
      { name: '上画待验收', time: '', status: 0 },
      { name: '下画待验收', time: '', status: 0 },
      { name: '订单完成', time: '', status: 0 },
    ],
    uiReason: '',
    uiStartDate: '2019-03-12',
    toggle1: true,
    toggle2: true
  },
  handleToggle1() {
    this.setData({ toggle1: !this.data.toggle1 });
  },
  handleToggle2() {
    this.setData({ toggle2: !this.data.toggle2 });
  },
  onLoad(option) {
    console.log('activity detail onLoad')
    const { activity_id, order_no } = option
    this.getActivityDetail({ activity_id })
    this.getOrderDetailByOrderNo({ order_no })
    this.getOrderStatusList({ order_no })
  },
  handleImageView(e) {
    const { activity_image_url, activity_image_name } = e.currentTarget.dataset.detail
    const urls = [{
      name: activity_image_name,
      url: activity_image_url,
    }]
    this.prepareEffectImage(urls)
    global.handleRoute({ route: 'effect' })
  },
  handleInput(e) {
    // console.log('handleInput', e)
    const { field } = e.currentTarget.dataset
    const { value } = e.detail
    const obj = {}
    obj[field] = value
    this.setData(obj)
  },
  handleDialog1Cancel(e) {
    this.selectComponent('#dialog1').handleHide()
  },
  handleDialog2Cancel(e) {
    this.selectComponent('#dialog2').handleHide()
  },
  handleDialog1Confirm(e) {
    if (!this.data.uiReason) {
      this.selectComponent('#toast').handleShow({
        message: '请填写不通过的原因'
      })
      return
    }
    this.handleDialog1Cancel()
    this.postAuditOrder({
      order_no: this.data.orderDetail.data.order_no,
      user_id: this.data.minaLoginRes.data.user_id,
      status: 2,
      reason: this.data.uiReason
    }).then(res => {
      if (res.actionStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 500)
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '操作失败，请稍后再试'
        })
      }
    }).catch(err => {
      this.selectComponent('#toast').handleShow({
        message: res.apiMes || '操作失败，请稍后再试'
      })
    })
  },
  handleDialog2Confirm(e) {
    this.handleDialog2Cancel()
  },
  handleLicon1Tap(e) {
    wx.navigateBack()
  },
  handleTabTap(e) {
    this.setData({
      uiTabIndex: e.currentTarget.dataset.index
    })
  },
  handleOpenDialog1(e) {
    this.selectComponent('#dialog1').handleShow()
  },
  handleOrderStatus(e) {
    const { status } = e.currentTarget.dataset
    switch (status) {
      case 0:
        this.selectComponent('#dialog2').handleShow(null, {
          message: '确定通过上画审核'
        })
        break;
      case 1:
        this.selectComponent('#dialog2').handleShow(null, {
          message: '确定通过下画审核'
        })
        break;
      default:
        break;
    }
  },
  // 通过审核
  handlePostAuditOrder(e) {
    console.log('handlePostAuditOrder', e)
    this.postAuditOrder({
      order_no: this.data.orderDetail.data.order_no,
      user_id: this.data.minaLoginRes.data.user_id,
      status: 3,
      reason: ''
    }).then(res => {
      if (res.actionStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 500)
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '操作失败，请稍后再试'
        })
      }
    }).catch(err => {
      this.selectComponent('#toast').handleShow({
        message: res.apiMes || '操作失败，请稍后再试'
      })
    })
  },
  // 修改订单
  handleReMakeOrder(e) {
    console.log('handleReMakeOrder', e)
    global.handleRoute({
      route: 'make-order-step1',
      query: {
        order_no: this.data.orderDetail.data.order_no,
        store_id: this.data.orderDetail.data.store_id,
        update: true,
      },
      type: 'redirectTo'
    })
  },


  // 物料领取 弹框
  handleDialog4Cancel(e) {
    console.log('handleDialog4Cancel', e)
    this.selectComponent('#dialog4').handleHide()
  },
  handleDialog4Confirm(e) {
    console.log('handleDialog4Confirm', e)
    const { order_no } = this.data.orderDetail.data
    const user_id = this.data.minaLoginRes.data.id
    const status = this.data.orderDetail.data.order_status
    const formData = { order_no, user_id, status }
    const stepName = '领取物料'
    apiSetOrderStatus(formData).then(res => {
      console.log(res)
      if (res.apiStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes || `${stepName}成功`
        })
        this.getOrderDetailByOrderNo({ order_no })
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || `${stepName}失败，请稍后再试`
        })
      }
    }).catch(err => {
      console.error(err)
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || `${stepName}失败，请稍后再试`
      })
    })
    this.selectComponent('#dialog4').handleHide()
  },
  handleOpenDialog4(e) {
    console.log('handleOpenDialog4', e)
    this.selectComponent('#dialog4').handleShow(null, {
      message: '确定物料领取？'
    })
  },
  // 确认到店 弹框
  handleDialog5Cancel(e) {
    console.log('handleDialog5Cancel', e)
    this.selectComponent('#dialog5').handleHide()
  },
  handleDialog5Confirm(e) {
    console.log('handleDialog5Confirm', e)
    const { order_no } = this.data.orderDetail.data
    const user_id = this.data.minaLoginRes.data.id
    const status = this.data.orderDetail.data.order_status
    const formData = { order_no, user_id, status }
    const stepName = '确认到店'
    apiSetOrderStatus(formData).then(res => {
      console.log(res)
      if (res.apiStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes || `${stepName}成功`
        })
        this.getOrderDetailByOrderNo({ order_no })
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || `${stepName}失败，请稍后再试`
        })
      }
    }).catch(err => {
      console.error(err)
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || `${stepName}失败，请稍后再试`
      })
    })
    this.selectComponent('#dialog5').handleHide()
  },
  handleOpenDialog5(e) {
    console.log('handleOpenDialog5', e)
    this.selectComponent('#dialog5').handleShow(null, {
      message: '确定到店？'
    })
  },
  // 完成上画 弹框
  handleDialog6Cancel(e) {
    console.log('handleDialog6Cancel', e)
    this.selectComponent('#dialog6').handleHide()
  },
  handleDialog6Confirm(e) {
    console.log('handleDialog4Confirm', e)
    const { order_no } = this.data.orderDetail.data
    const user_id = this.data.minaLoginRes.data.id
    const status = this.data.orderDetail.data.order_status
    const formData = { order_no, user_id, status }
    const stepName = '完成上画'
    apiSetOrderStatus(formData).then(res => {
      console.log(res)
      if (res.apiStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes || `${stepName}成功`
        })
        this.getOrderDetailByOrderNo({ order_no })
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || `${stepName}失败，请稍后再试`
        })
      }
    }).catch(err => {
      console.error(err)
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || `${stepName}失败，请稍后再试`
      })
    })
    this.selectComponent('#dialog6').handleHide()
  },
  handleOpenDialog6(e) {
    console.log('handleOpenDialog6', e)
    this.selectComponent('#dialog6').handleShow(null, {
      message: '确定完成上画？'
    })
  },
  // 完成下画 弹框
  handleDialog7Cancel(e) {
    console.log('handleDialog7Cancel', e)
    this.selectComponent('#dialog7').handleHide()
  },
  handleDialog7Confirm(e) {
    console.log('handleDialog7Confirm', e)
    const { order_no } = this.data.orderDetail.data
    const user_id = this.data.minaLoginRes.data.id
    const status = this.data.orderDetail.data.order_status
    const formData = { order_no, user_id, status }
    const stepName = '完成下画'
    apiSetOrderStatus(formData).then(res => {
      console.log(res)
      if (res.apiStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: res.apiMes || `${stepName}成功`
        })
        this.getOrderDetailByOrderNo({ order_no })
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || `${stepName}失败，请稍后再试`
        })
      }
    }).catch(err => {
      console.error(err)
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || `${stepName}失败，请稍后再试`
      })
    })
    this.selectComponent('#dialog7').handleHide()
  },
  handleOpenDialog7(e) {
    console.log('handleOpenDialog7', e)
    this.selectComponent('#dialog7').handleShow(null, {
      message: '确定完成下画？'
    })
  },
}))
